package com.lxl.workbench.dao;



import com.lxl.commons.utils.DbUtil;
import com.lxl.workbench.entity.Category;


import javax.servlet.jsp.jstl.sql.Result;
import java.sql.SQLException;


/**
 * @author 北上寻海
 */
public class CategoryDaoImpl implements CategoryDao {
    @Override
    public Result selectBookCategory(String bookCategory) throws SQLException {
        String sql = "select id,book_type from tbl_category where book_type like '%"+bookCategory+"%'";
        return DbUtil.select(sql);
    }

    @Override
    public void deleteCategoryByIds(String[] delIds) throws SQLException {
        StringBuilder sql1 = new StringBuilder("delete from tbl_category where id in ");
        sql1.append("(");
        for (String delId : delIds) {
            sql1.append("'").append(delId).append("',");
        }
        sql1 = new StringBuilder(sql1.substring(0, sql1.length() - 1));
        sql1.append(")");

        StringBuilder sql2 = new StringBuilder("delete from tbl_book where type_id in ");
        sql2.append("(");
        for (String delId : delIds) {
            sql2.append("'").append(delId).append("',");
        }
        sql2 = new StringBuilder(sql2.substring(0, sql2.length() - 1));
        sql2.append(")");

        StringBuilder sql3 = new StringBuilder("update tbl_category_staff set manage_type = null where manage_type in ");
        sql3.append("(");
        for (String delId : delIds) {
            sql3.append("'").append(delId).append("',");
        }
        sql3 = new StringBuilder(sql3.substring(0, sql3.length() - 1));
        sql3.append(")");

        DbUtil.update(sql1.toString(),sql2.toString(),sql3.toString());
    }

    @Override
    public int addCategoryByCondition(Category category) throws SQLException {
        String sql = "insert into tbl_category values ('"+category.getId()+"','"+category.getBookType()+"')";
        return DbUtil.update(sql);
    }

    @Override
    public Result selectCategoryId(String bookType) throws SQLException {
        String sql = "select id,book_type from tbl_category where book_type = ?";
        return DbUtil.select(sql,bookType);
    }
}
